﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Commands.User.Commands.Order;
using DotNet.Common.Command;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using NHibernate;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.Shop.OrderReturns
{
    public partial class OrderReturnsRefund : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected string _userAccount;
        string oid;
        protected ShopOrderInfo OrderData { get; set; }
        protected ShopOrderProduct OrderProductData { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            if (!IsPostBack)
            {
                oid = Request["id"] == null ? "" : Request["id"];
                _oid.Text = oid;
                _orderNo.Text = Request["orderno"];
                _pNo.Text = Request["pno"];
                _skuNo.Text = Request["skuno"];
            }
            else
            {
                oid = _oid.Text;
                Save();
            }

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                OrderData = session.CreateQuery("from ShopOrderInfo where OrderNo=:no")
                        .SetParameter("no", _orderNo.Text)
                        .UniqueResult<ShopOrderInfo>();
                if (OrderData != null)
                {

                    OrderProductData = session.CreateQuery("from ShopOrderProduct where OrderNo=:no and ProductNo=:pno and SkuProductNo=:skuno ")
                            .SetParameter("no", OrderData.OrderNo)
                            .SetParameter("pno", _pNo.Text)
                            .SetParameter("skuno", _skuNo.Text)
                            .UniqueResult<ShopOrderProduct>();
                }
            };
            if (OrderData == null || OrderProductData == null)
            {
                _framePage.Alert(new ReturnCode()
                {
                    statusCode = 300,
                    message = "操作失败!参数错误!",
                    callbackType = "closeCurrent",
                    navTabId = "OrderReturnsDetail"
                });
            }
        }

        protected void Save()
        {
            decimal rfAmout = Convert.ToDecimal(tb_amount.Text);
            if (rfAmout > 0)
            {

                ICommandResult<RefundOrderResult> res = new RefundOrderCommand().Execute(new RefundOrderParameter()
                {
                    OrderNo = _orderNo.Text,
                    ProductSkuNo = _skuNo.Text,
                    ProductNo = _pNo.Text,
                    RefundKind = "REFUND",
                    UserAccount = _userAccount,
                    RefundAmount = rfAmout,
                    Memo = tb_memo.Text
                });
                if (res.Error)
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "操作失败!",
                        callbackType = "closeCurrent",
                        navTabId = "OrderReturnsDetail"
                    });
                }
                else
                {
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        session.CreateSQLQuery("update Shop_Order_Returns set refund_no=:rf where recid=:id ")
                            .SetParameter("rf", res.Result.RefundNo)
                            .SetParameter("id", _oid.Text)
                            .UniqueResult();
                        OrderReturnsUtil.WriteLog(session, int.Parse(_oid.Text), "退款处理中", (string.IsNullOrEmpty(tb_memo.Text) ? "无" : tb_memo.Text), _userAccount);
                    };

                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 200,
                        message = "操作成功!",
                        callbackType = "closeCurrent",
                        navTabId = "OrderReturnsDetail"
                    });
                }
            }
            else
            {
                _framePage.Alert(new ReturnCode()
                {
                    statusCode = 300,
                    message = "退款金额不能小于0"
                });
            }

        }
    }
}
